﻿using Microsoft.EntityFrameworkCore;
using Books.Models;
namespace DataAccess
{
    public class BooksContext : DbContext
    {
        public BooksContext()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionString = "Data Source=localhost;Initial Catalog=BookMangers;User ID=root;Password=xwd+=527";
            optionsBuilder.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 30)));

            base.OnConfiguring(optionsBuilder);
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Book>().HasKey(c => c.BookId);
            modelBuilder.Entity<User>().HasKey(c=>c.UserID);
            modelBuilder.Entity<Admin>().HasKey(c=>c.AdminID);
            modelBuilder.Entity<BorrowRecord>(entity=>{
                entity.HasKey(c => c.RecordID);
                entity.HasOne(c => c.book).WithMany(c => c.borrowRecords).HasForeignKey(c => c.BookID);
                entity.HasOne(c => c.user).WithMany(c => c.borrowRecords).HasForeignKey(c => c.UserID);
                });

           

            base.OnModelCreating(modelBuilder);
        }

        public DbSet<Book> Books { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Admin> Admins { get; set; }
        public DbSet<BorrowRecord> BorrowRecords { get; set; }
    }
}